package demoMod.nanaki.patches.events.city;

import basemod.ReflectionHacks;
import com.evacipated.cardcrawl.modthespire.lib.SpirePatch;
import com.evacipated.cardcrawl.modthespire.lib.SpireReturn;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.events.AbstractImageEvent;
import com.megacrit.cardcrawl.events.city.CursedTome;
import com.megacrit.cardcrawl.localization.EventStrings;
import com.megacrit.cardcrawl.relics.AbstractRelic;
import com.megacrit.cardcrawl.relics.Enchiridion;
import com.megacrit.cardcrawl.relics.Necronomicon;
import com.megacrit.cardcrawl.relics.NilrysCodex;
import com.megacrit.cardcrawl.rooms.AbstractRoom;
import demoMod.nanaki.NanakiMod;
import demoMod.nanaki.characters.NanakiCharacter;

import java.util.ArrayList;
import java.util.List;

public class CursedTomePatch {
    public static final String ID = NanakiMod.makeID("Cursed Tome");
    private static final EventStrings eventStrings;
    public static final String NAME;
    public static final String[] DESCRIPTIONS;
    public static final String[] OPTIONS;

    @SpirePatch(
            clz = CursedTome.class,
            method = SpirePatch.CONSTRUCTOR
    )
    public static class PatchConstructor {
        public static void Postfix(CursedTome event) {
            AbstractPlayer p = AbstractDungeon.player;
            if (p instanceof NanakiCharacter) {
                int maxHpToLose = AbstractDungeon.ascensionLevel >= 15 ? p.maxHealth / 2 : (int) (p.maxHealth * 0.3F);
                event.imageEventText.setDialogOption(String.format(OPTIONS[8], maxHpToLose));
            }
        }
    }

    @SpirePatch(
            clz = CursedTome.class,
            method = "buttonEffect"
    )
    public static class PatchButtonEffect {
        private static boolean choosePurify;
        private static boolean chooseBook;

        public static SpireReturn<Void> Prefix(CursedTome event, int buttonPressed) {
            if (AbstractDungeon.player instanceof NanakiCharacter) {
                try {
                    AbstractPlayer p = AbstractDungeon.player;
                    int maxHpToLose = AbstractDungeon.ascensionLevel >= 15 ? p.maxHealth / 2 : (int) (p.maxHealth * 0.3F);
                    Enum screen = ReflectionHacks.getPrivate(event, CursedTome.class, "screen");
                    if ("INTRO".equals(screen.name())) {
                        choosePurify = false;
                        chooseBook = false;
                        if (buttonPressed == 2) {
                            AbstractDungeon.player.decreaseMaxHealth(maxHpToLose);
                            event.imageEventText.updateBodyText(DESCRIPTIONS[8]);
                            event.imageEventText.clearAllDialogs();
                            List<AbstractRelic> books = new ArrayList<>();
                            books.add(new Necronomicon());
                            books.add(new Enchiridion());
                            books.add(new NilrysCodex());
                            for (AbstractRelic book : books) {
                                String displayName = book.name.replaceAll(" ", " #g");
                                event.imageEventText.setDialogOption(String.format(OPTIONS[9], displayName), book);
                            }
                            ReflectionHacks.setPrivate(event, CursedTome.class, "screen", Enum.valueOf((Class<Enum>) Class.forName("com.megacrit.cardcrawl.events.city.CursedTome$CurScreen"), "END"));
                            choosePurify = true;
                            return SpireReturn.Return(null);
                        }
                    } else if ("END".equals(screen.name()) && choosePurify) {
                        if (!chooseBook) {
                            AbstractRelic r;
                            switch (buttonPressed) {
                                case 0:
                                    r = new Necronomicon();
                                    break;
                                case 1:
                                    r = new Enchiridion();
                                    break;
                                default:
                                    r = new NilrysCodex();
                            }
                            AbstractImageEvent.logMetricTakeDamage("Cursed Tome", "Obtained Book", maxHpToLose);
                            AbstractDungeon.getCurrRoom().rewards.clear();
                            AbstractDungeon.getCurrRoom().addRelicToRewards(r);
                            AbstractDungeon.getCurrRoom().phase = AbstractRoom.RoomPhase.COMPLETE;
                            AbstractDungeon.combatRewardScreen.open();
                            event.imageEventText.clearAllDialogs();
                            event.imageEventText.setDialogOption(OPTIONS[7]);
                            chooseBook = true;
                            return SpireReturn.Return(null);
                        }
                    }
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
            return SpireReturn.Continue();
        }
    }

    static {
        eventStrings = CardCrawlGame.languagePack.getEventString(ID);
        NAME = eventStrings.NAME;
        DESCRIPTIONS = eventStrings.DESCRIPTIONS;
        OPTIONS = eventStrings.OPTIONS;
    }
}
